<!DOCTYPE html>


<html lang="zh-CN">


<head>
  <meta charset="utf-8" />
    
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
  <title>
    Jenkis pipeline编写podTemplate实现和k8s对接.md |  
  </title>
  <meta name="generator" content="hexo-theme-ayer">
  
  <link rel="shortcut icon" href="/favicon.ico" />
  
  
<link rel="stylesheet" href="/dist/main.css">

  
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/Shen-Yu/cdn/css/remixicon.min.css">

  
<link rel="stylesheet" href="/css/custom.css">

  
  
<script src="https://cdn.jsdelivr.net/npm/pace-js@1.0.2/pace.min.js"></script>

  
  

  

</head>

</html>

<body>
  <div id="app">
    
      
    <main class="content on">
      <section class="outer">
  <article
  id="post-k8s/Jenkis pipeline编写podTemplate实现和k8s对接"
  class="article article-type-post"
  itemscope
  itemprop="blogPost"
  data-scroll-reveal
>
  <div class="article-inner">
    
    <header class="article-header">
       
<h1 class="article-title sea-center" style="border-left:0" itemprop="name">
  Jenkis pipeline编写podTemplate实现和k8s对接.md
</h1>
 

    </header>
     
    <div class="article-meta">
      <a href="/2020/11/11/k8s/Jenkis%20pipeline%E7%BC%96%E5%86%99podTemplate%E5%AE%9E%E7%8E%B0%E5%92%8Ck8s%E5%AF%B9%E6%8E%A5/" class="article-date">
  <time datetime="2020-11-10T16:00:00.000Z" itemprop="datePublished">2020-11-11</time>
</a> 
  <div class="article-category">
    <a class="article-category-link" href="/categories/k8s/">k8s</a>
  </div>
  
<div class="word_count">
    <span class="post-time">
        <span class="post-meta-item-icon">
            <i class="ri-quill-pen-line"></i>
            <span class="post-meta-item-text"> 字数统计:</span>
            <span class="post-count">2.6k</span>
        </span>
    </span>

    <span class="post-time">
        &nbsp; | &nbsp;
        <span class="post-meta-item-icon">
            <i class="ri-book-open-line"></i>
            <span class="post-meta-item-text"> 阅读时长≈</span>
            <span class="post-count">14 分钟</span>
        </span>
    </span>
</div>
 
    </div>
      
    <div class="tocbot"></div>




  
    <div class="article-entry" itemprop="articleBody">
       
  <h1 id="Jenkis-pipeline编写podTemplate实现和k8s对接"><a href="#Jenkis-pipeline编写podTemplate实现和k8s对接" class="headerlink" title="Jenkis pipeline编写podTemplate实现和k8s对接"></a>Jenkis pipeline编写podTemplate实现和k8s对接</h1><blockquote>
<p>在完成前文的jenkins server 在k8s环境部署之后，本文我们来测试在k8s集群环境中的jenkins pipeline构建项目和更新，具体环境要求如下：<br>1、jenkins pipeline插件安装成功<br>2、要更新的应用已提前部署<br>3、Jenkins slave中需要有kubectl、svn、mvn客户端且环境变量设置准确<br>4、Jenkis slave需要能和master的api-server进行正常通信（这里为了简便，前文构建jenkins server的时候直接对defalut这个service account做rabc授权。其他可选方案是使用~/.kube/config文件实现客户端的授权认证）<br>5、需要配置共享卷(slave编译完成之后把war包copy到共享卷位置)</p>
</blockquote>
<h1 id="一、创建构建任务"><a href="#一、创建构建任务" class="headerlink" title="一、创建构建任务"></a>一、创建构建任务</h1><p>1、点击“jenkins ”——“新建 ”——“pipeline”，输入项目的名称，点击“OK”<br><img src="http://iubest.gitee.io/pic/640-1601168587754.webp" alt="img"></p>
<p>2、下拉到“pipeline”配置，填写脚本<br><img src="http://iubest.gitee.io/pic/640-1601168618729.webp" alt="img"><br>具体脚本内容如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line">podTemplate(name: &#39;jenkins-slave&#39;, cloud: &#39;kubernetes&#39;,</span><br><span class="line">  namespace: &#39;default&#39;, label: &#39;jenkins-slave&#39;,</span><br><span class="line">  serviceAccount: &#39;default&#39;, containers: [</span><br><span class="line">  containerTemplate(</span><br><span class="line">      name: &#39;jenkins-slave&#39;,</span><br><span class="line">      image: &#39;harbor.59iedu.com&#x2F;fjhb&#x2F;jenkins-slave-toolkit:2018-08-10-v1&#39;,</span><br><span class="line">      args: &#39;$&#123;computer.jnlpmac&#125; $&#123;computer.name&#125;&#39;,</span><br><span class="line">      ttyEnabled: true,</span><br><span class="line">      privileged: false,</span><br><span class="line">      alwaysPullImage:true,</span><br><span class="line">      )</span><br><span class="line">  ],</span><br><span class="line">  volumes: [</span><br><span class="line">    persistentVolumeClaim(mountPath: &#39;&#x2F;tmp&#x2F;&#39;, claimName: &#39;tomcat-jcsj-data&#39;)</span><br><span class="line">  ]) &#123;</span><br><span class="line">  node(&#39;jenkins-slave&#39;) &#123;</span><br><span class="line">    stage(&#39;svn-checkout&#39;) &#123;</span><br><span class="line">      container(&#39;jnlp&#39;) &#123;</span><br><span class="line">          sh &quot;&quot;&quot;</span><br><span class="line">          svn checkout --username&#x3D;yanglw --password&#x3D;mypassword http:&#x2F;&#x2F;192.168.1.81&#x2F;svn&#x2F;fjhbjsb&#x2F;k8s-pipeline-test --non-interactive </span><br><span class="line">          &quot;&quot;&quot;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    stage(&#39;mvn-package&#39;) &#123;</span><br><span class="line">      container(&#39;jnlp&#39;) &#123;</span><br><span class="line">          sh &quot;&quot;&quot;</span><br><span class="line">          mkdir -p &#x2F;var&#x2F;jenkins_home &amp;&amp; cd k8s-pipeline-test &amp;&amp; mvn clean package &amp;&amp; cp -rpf target&#x2F;*.war &#x2F;tmp&#x2F;</span><br><span class="line">          &quot;&quot;&quot;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    stage(&#39;restart&#39;) &#123;</span><br><span class="line">      container(&#39;jnlp&#39;) &#123;</span><br><span class="line">          sh &quot;&quot;&quot;</span><br><span class="line">          pod_name&#x3D;&#96;kubectl  get pods -l name&#x3D;jcsj-dev -o name | cut -d&quot;&#x2F;&quot; -f2&#96;</span><br><span class="line">          kubectl delete pod \$pod_name</span><br><span class="line">          &quot;&quot;&quot;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>3、脚本释义<br>podTemplate节指定创建pod的模板和环境</p>
<blockquote>
<p>Name：为pod的名称前缀<br>Cloud：为构建pod的云环境，需要和前面新建的云环境名称一样<br>Namespace：创建pod所在的namespace<br>Label: 创建pod对应的标签<br>serviceAccount：pod使用sa，这里使用default, 所以前文创建jenkins master deployment的时候多创建了一个default-role,这样自动创建出来的的jenkins-slave具有对应的api权限</p>
</blockquote>
<p>containerTemplate节</p>
<blockquote>
<p>指定创建容器的模板，当云环境里面配置pod模板后，容器模板以云环境的配置为准。</p>
</blockquote>
<p>Volumes节</p>
<blockquote>
<p>配置jenkins-slave pod挂载的卷，当云环境里面配置pod模板后，挂载卷以云环境配置为准</p>
</blockquote>
<p>stage节为具体的pipeline步骤</p>
<blockquote>
<p>这里第一步进行svn代码迁出；<br>第二步进行编译，并把包传到共享卷上面；<br>第三步对pod进行删除，因为应用采用deployment方式部署，所以可以实现重新创建pod，达到更新的效果。</p>
</blockquote>
<h1 id="二、构建准备"><a href="#二、构建准备" class="headerlink" title="二、构建准备"></a>二、构建准备</h1><p>在开始构建之前，有必要介绍一下jcsj-dev的环境，这个环境是需要提前部署好的。部署文件如下</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br></pre></td><td class="code"><pre><span class="line"># cat deploy.yml </span><br><span class="line">---</span><br><span class="line">apiVersion: extensions&#x2F;v1beta1</span><br><span class="line">kind: Deployment</span><br><span class="line">metadata:</span><br><span class="line">  name: jcsj-dev</span><br><span class="line">  namespace: default</span><br><span class="line">spec:</span><br><span class="line">  replicas: 1</span><br><span class="line">  template:</span><br><span class="line">    metadata:</span><br><span class="line">      labels:</span><br><span class="line">        name: jcsj-dev</span><br><span class="line">    spec:</span><br><span class="line">      containers:</span><br><span class="line">      - name: jcsj-dev</span><br><span class="line">        image: tomcat:latest</span><br><span class="line">        imagePullPolicy: IfNotPresent</span><br><span class="line">        ports:</span><br><span class="line">        - name: web</span><br><span class="line">          containerPort: 8080</span><br><span class="line">        volumeMounts:</span><br><span class="line">        - mountPath: &#x2F;usr&#x2F;local&#x2F;tomcat&#x2F;webapps</span><br><span class="line">          name: tomcat-jcsj-data</span><br><span class="line">      volumes:</span><br><span class="line">      - name: tomcat-jcsj-data</span><br><span class="line">        persistentVolumeClaim:</span><br><span class="line">          claimName: tomcat-jcsj-data</span><br><span class="line">---</span><br><span class="line">apiVersion: v1</span><br><span class="line">kind: Service</span><br><span class="line">metadata:</span><br><span class="line">  name: jcsj-dev</span><br><span class="line">spec:</span><br><span class="line">  type: NodePort</span><br><span class="line">  ports:</span><br><span class="line">    - port: 8080</span><br><span class="line">      targetPort: 8080</span><br><span class="line">      nodePort: 8453</span><br><span class="line">  selector:</span><br><span class="line">name: jcsj-dev</span><br><span class="line"># cat pv.yaml </span><br><span class="line">---</span><br><span class="line">apiVersion: v1</span><br><span class="line">kind: PersistentVolume</span><br><span class="line">metadata:</span><br><span class="line">  name: tomcat-jcsj-data</span><br><span class="line">spec:</span><br><span class="line">  capacity:</span><br><span class="line">    storage: 5Gi </span><br><span class="line">  accessModes:</span><br><span class="line">  - ReadWriteMany </span><br><span class="line">  nfs: </span><br><span class="line">    path: &#x2F;home&#x2F;tomcat_jcsj</span><br><span class="line">    server: 192.168.115.6</span><br><span class="line">  persistentVolumeReclaimPolicy: Recycle </span><br><span class="line"></span><br><span class="line">---</span><br><span class="line">kind: PersistentVolumeClaim</span><br><span class="line">apiVersion: v1</span><br><span class="line">metadata:</span><br><span class="line">  name: tomcat-jcsj-data</span><br><span class="line">spec:</span><br><span class="line">  accessModes:</span><br><span class="line">    - ReadWriteMany</span><br><span class="line">  resources:</span><br><span class="line">    requests:</span><br><span class="line">      storage: 5Gi</span><br></pre></td></tr></table></figure>

<p><img src="http://iubest.gitee.io/pic/640-1601168590868.webp" alt="img"><br><img src="http://iubest.gitee.io/pic/640-1601168587505.webp" alt="img"></p>
<h1 id="三、更新项目代码"><a href="#三、更新项目代码" class="headerlink" title="三、更新项目代码"></a>三、更新项目代码</h1><p>更新index.jsp并提交<br><img src="http://iubest.gitee.io/pic/640-1601168590633.webp" alt="img"><br><img src="http://iubest.gitee.io/pic/640-1601168590634.webp" alt="img"></p>
<h1 id="四、开始构建"><a href="#四、开始构建" class="headerlink" title="四、开始构建"></a>四、开始构建</h1><p>点击“立即构建”，构建过程中可以通过控制台输出看具体的日志信息<br><img src="http://iubest.gitee.io/pic/640-1601168587499.webp" alt="img"><br><img src="http://iubest.gitee.io/pic/640-1601168590869.webp" alt="img"><br><img src="http://iubest.gitee.io/pic/640-1601168587775.webp" alt="img"></p>
<p>构建过程中观察jenkins master的日志输出：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br></pre></td><td class="code"><pre><span class="line"># kubectl logs -f jenkins-master-588b89c75f-ztvgm </span><br><span class="line">INFO: Started provisioning Kubernetes Pod Template from kubernetes with 1 executors. Remaining excess workload: 0</span><br><span class="line">Aug 13, 2018 10:14:22 AM hudson.slaves.NodeProvisioner$2 run</span><br><span class="line">INFO: Kubernetes Pod Template provisioning successfully completed. We have now 2 computer(s)</span><br><span class="line">Aug 13, 2018 10:14:22 AM org.csanchez.jenkins.plugins.kubernetes.KubernetesLauncher launch</span><br><span class="line">INFO: Created Pod: jenkins-slave-gjjbh in namespace default</span><br><span class="line">Aug 13, 2018 10:14:22 AM org.csanchez.jenkins.plugins.kubernetes.KubernetesLauncher launch</span><br><span class="line">INFO: Waiting for Pod to be scheduled (0&#x2F;100): jenkins-slave-gjjbh</span><br><span class="line">Aug 13, 2018 10:14:28 AM org.csanchez.jenkins.plugins.kubernetes.KubernetesLauncher launch</span><br><span class="line">INFO: Container is waiting jenkins-slave-gjjbh [jnlp]: ContainerStateWaiting(message&#x3D;null, reason&#x3D;ContainerCreating, additionalProperties&#x3D;&#123;&#125;)</span><br><span class="line">Aug 13, 2018 10:14:28 AM org.csanchez.jenkins.plugins.kubernetes.KubernetesLauncher launch</span><br><span class="line">INFO: Waiting for Pod to be scheduled (1&#x2F;100): jenkins-slave-gjjbh</span><br><span class="line">Aug 13, 2018 10:14:34 AM org.csanchez.jenkins.plugins.kubernetes.KubernetesLauncher launch</span><br><span class="line">INFO: Waiting for agent to connect (1&#x2F;100): jenkins-slave-gjjbh</span><br><span class="line">Aug 13, 2018 10:14:35 AM org.csanchez.jenkins.plugins.kubernetes.KubernetesLauncher launch</span><br><span class="line">INFO: Waiting for agent to connect (2&#x2F;100): jenkins-slave-gjjbh</span><br><span class="line">Aug 13, 2018 10:14:36 AM org.csanchez.jenkins.plugins.kubernetes.KubernetesLauncher launch</span><br><span class="line">INFO: Waiting for agent to connect (3&#x2F;100): jenkins-slave-gjjbh</span><br><span class="line">Aug 13, 2018 10:14:37 AM org.csanchez.jenkins.plugins.kubernetes.KubernetesLauncher launch</span><br><span class="line">INFO: Waiting for agent to connect (4&#x2F;100): jenkins-slave-gjjbh</span><br><span class="line">Aug 13, 2018 10:14:38 AM org.csanchez.jenkins.plugins.kubernetes.KubernetesLauncher launch</span><br><span class="line">INFO: Waiting for agent to connect (5&#x2F;100): jenkins-slave-gjjbh</span><br><span class="line">Aug 13, 2018 10:14:39 AM org.csanchez.jenkins.plugins.kubernetes.KubernetesLauncher launch</span><br><span class="line">INFO: Waiting for agent to connect (6&#x2F;100): jenkins-slave-gjjbh</span><br><span class="line">Aug 13, 2018 10:14:40 AM org.csanchez.jenkins.plugins.kubernetes.KubernetesLauncher launch</span><br><span class="line">INFO: Waiting for agent to connect (7&#x2F;100): jenkins-slave-gjjbh</span><br><span class="line">Aug 13, 2018 10:14:41 AM org.csanchez.jenkins.plugins.kubernetes.KubernetesLauncher launch</span><br><span class="line">INFO: Waiting for agent to connect (8&#x2F;100): jenkins-slave-gjjbh</span><br><span class="line">Aug 13, 2018 10:14:42 AM hudson.TcpSlaveAgentListener$ConnectionHandler run</span><br><span class="line">INFO: Accepted JNLP4-connect connection #1 from &#x2F;172.30.66.4:47408</span><br><span class="line">Aug 13, 2018 10:14:42 AM org.csanchez.jenkins.plugins.kubernetes.KubernetesLauncher launch</span><br><span class="line">INFO: Waiting for agent to connect (9&#x2F;100): jenkins-slave-gjjbh</span><br><span class="line">Aug 13, 2018 10:14:43 AM org.csanchez.jenkins.plugins.kubernetes.KubernetesLauncher launch</span><br><span class="line">INFO: Waiting for agent to connect (10&#x2F;100): jenkins-slave-gjjbh</span><br><span class="line">Aug 13, 2018 10:14:44 AM org.csanchez.jenkins.plugins.kubernetes.KubernetesLauncher launch</span><br><span class="line">INFO: Waiting for agent to connect (11&#x2F;100): jenkins-slave-gjjbh</span><br><span class="line">Aug 13, 2018 10:14:45 AM org.csanchez.jenkins.plugins.kubernetes.KubernetesLauncher launch</span><br><span class="line">INFO: Waiting for agent to connect (12&#x2F;100): jenkins-slave-gjjbh</span><br><span class="line">Aug 13, 2018 10:14:46 AM org.csanchez.jenkins.plugins.kubernetes.KubernetesLauncher launch</span><br><span class="line">INFO: Waiting for agent to connect (13&#x2F;100): jenkins-slave-gjjbh</span><br><span class="line">Aug 13, 2018 10:14:47 AM org.csanchez.jenkins.plugins.kubernetes.KubernetesLauncher launch</span><br><span class="line">INFO: Waiting for agent to connect (14&#x2F;100): jenkins-slave-gjjbh</span><br><span class="line">Aug 13, 2018 10:14:48 AM org.csanchez.jenkins.plugins.kubernetes.KubernetesLauncher launch</span><br><span class="line">INFO: Waiting for agent to connect (15&#x2F;100): jenkins-slave-gjjbh</span><br><span class="line">Aug 13, 2018 10:14:49 AM org.csanchez.jenkins.plugins.kubernetes.KubernetesLauncher launch</span><br><span class="line">INFO: Waiting for agent to connect (16&#x2F;100): jenkins-slave-gjjbh</span><br><span class="line">Aug 13, 2018 10:14:50 AM org.csanchez.jenkins.plugins.kubernetes.KubernetesLauncher launch</span><br><span class="line">INFO: Waiting for agent to connect (17&#x2F;100): jenkins-slave-gjjbh</span><br><span class="line">Aug 13, 2018 10:14:51 AM org.csanchez.jenkins.plugins.kubernetes.KubernetesLauncher launch</span><br><span class="line">INFO: Waiting for agent to connect (18&#x2F;100): jenkins-slave-gjjbh</span><br><span class="line">Aug 13, 2018 10:14:52 AM org.csanchez.jenkins.plugins.kubernetes.KubernetesLauncher launch</span><br><span class="line">INFO: Waiting for agent to connect (19&#x2F;100): jenkins-slave-gjjbh</span><br><span class="line">Aug 13, 2018 10:14:53 AM org.csanchez.jenkins.plugins.kubernetes.KubernetesLauncher launch</span><br><span class="line">INFO: Waiting for agent to connect (20&#x2F;100): jenkins-slave-gjjbh</span><br><span class="line">Aug 13, 2018 10:15:12 AM org.csanchez.jenkins.plugins.kubernetes.pipeline.ContainerExecDecorator$1 doLaunch</span><br><span class="line">INFO: Created process inside pod: [jenkins-slave-gjjbh], container: [jnlp] with pid:[-1]</span><br><span class="line">Aug 13, 2018 10:15:21 AM org.csanchez.jenkins.plugins.kubernetes.pipeline.ContainerExecDecorator$1 doLaunch</span><br><span class="line">INFO: Created process inside pod: [jenkins-slave-gjjbh], container: [jnlp] with pid:[-1]</span><br><span class="line">Aug 13, 2018 10:16:04 AM org.csanchez.jenkins.plugins.kubernetes.pipeline.ContainerExecDecorator$1 doLaunch</span><br><span class="line">INFO: Created process inside pod: [jenkins-slave-gjjbh], container: [jnlp] with pid:[-1]</span><br><span class="line">Aug 13, 2018 10:16:06 AM org.csanchez.jenkins.plugins.kubernetes.KubernetesSlave _terminate</span><br><span class="line">INFO: Terminating Kubernetes instance for agent jenkins-slave-gjjbh</span><br><span class="line">Aug 13, 2018 10:16:06 AM jenkins.slaves.DefaultJnlpSlaveReceiver channelClosed</span><br><span class="line">WARNING: Computer.threadPoolForRemoting [#8] for jenkins-slave-gjjbh terminated</span><br><span class="line">java.nio.channels.ClosedChannelException</span><br><span class="line">        at org.jenkinsci.remoting.protocol.impl.ChannelApplicationLayer.onReadClosed(ChannelApplicationLayer.java:208)</span><br><span class="line">        at org.jenkinsci.remoting.protocol.ApplicationLayer.onRecvClosed(ApplicationLayer.java:222)</span><br><span class="line">        at org.jenkinsci.remoting.protocol.ProtocolStack$Ptr.onRecvClosed(ProtocolStack.java:832)</span><br><span class="line">        at org.jenkinsci.remoting.protocol.FilterLayer.onRecvClosed(FilterLayer.java:287)</span><br><span class="line">        at org.jenkinsci.remoting.protocol.impl.SSLEngineFilterLayer.onRecvClosed(SSLEngineFilterLayer.java:181)</span><br><span class="line">        at org.jenkinsci.remoting.protocol.impl.SSLEngineFilterLayer.switchToNoSecure(SSLEngineFilterLayer.java:283)</span><br><span class="line">        at org.jenkinsci.remoting.protocol.impl.SSLEngineFilterLayer.processWrite(SSLEngineFilterLayer.java:503)</span><br><span class="line">        at org.jenkinsci.remoting.protocol.impl.SSLEngineFilterLayer.processQueuedWrites(SSLEngineFilterLayer.java:248)</span><br><span class="line">        at org.jenkinsci.remoting.protocol.impl.SSLEngineFilterLayer.doSend(SSLEngineFilterLayer.java:200)</span><br><span class="line">        at org.jenkinsci.remoting.protocol.impl.SSLEngineFilterLayer.doCloseSend(SSLEngineFilterLayer.java:213)</span><br><span class="line">        at org.jenkinsci.remoting.protocol.ProtocolStack$Ptr.doCloseSend(ProtocolStack.java:800)</span><br><span class="line">        at org.jenkinsci.remoting.protocol.ApplicationLayer.doCloseWrite(ApplicationLayer.java:173)</span><br><span class="line">        at org.jenkinsci.remoting.protocol.impl.ChannelApplicationLayer$ByteBufferCommandTransport.closeWrite(ChannelApplicationLayer.java:311)</span><br><span class="line">        at hudson.remoting.Channel.close(Channel.java:1295)</span><br><span class="line">        at hudson.remoting.Channel.close(Channel.java:1263)</span><br><span class="line">        at hudson.slaves.SlaveComputer.closeChannel(SlaveComputer.java:708)</span><br><span class="line">        at hudson.slaves.SlaveComputer.access$800(SlaveComputer.java:96)</span><br><span class="line">        at hudson.slaves.SlaveComputer$3.run(SlaveComputer.java:626)</span><br><span class="line">        at jenkins.util.ContextResettingExecutorService$1.run(ContextResettingExecutorService.java:28)</span><br><span class="line">        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)</span><br><span class="line">        at java.util.concurrent.FutureTask.run(FutureTask.java:266)</span><br><span class="line">        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)</span><br><span class="line">        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)</span><br><span class="line">        at java.lang.Thread.run(Thread.java:748)</span><br><span class="line"></span><br><span class="line">Aug 13, 2018 10:16:06 AM org.csanchez.jenkins.plugins.kubernetes.pipeline.PodTemplateStepExecution$PodTemplateCallback finished</span><br><span class="line">INFO: Removing pod template and deleting pod jenkins-slave-7gnwx from cloud kubernetes</span><br><span class="line">Aug 13, 2018 10:16:06 AM org.csanchez.jenkins.plugins.kubernetes.KubernetesSlave _terminate</span><br><span class="line">INFO: Terminated Kubernetes instance for agent default&#x2F;jenkins-slave-gjjbh</span><br><span class="line">Aug 13, 2018 10:16:06 AM org.csanchez.jenkins.plugins.kubernetes.KubernetesSlave _terminate</span><br><span class="line">INFO: Disconnected computer jenkins-slave-gjjbh</span><br><span class="line">Terminated Kubernetes instance for agent default&#x2F;jenkins-slave-gjjbh</span><br><span class="line">Aug 13, 2018 10:16:06 AM org.csanchez.jenkins.plugins.kubernetes.pipeline.PodTemplateStepExecution$PodTemplateCallback finished</span><br><span class="line">WARNING: Failed to delete pod for agent default&#x2F;jenkins-slave-7gnwx: not found</span><br><span class="line">Aug 13, 2018 10:16:07 AM org.jenkinsci.plugins.workflow.job.WorkflowRun finish</span><br><span class="line">INFO: k8s-pipeline-test #1 completed: SUCCESS</span><br></pre></td></tr></table></figure>

<h1 id="五、验证结果"><a href="#五、验证结果" class="headerlink" title="五、验证结果"></a>五、验证结果</h1><p><img src="http://iubest.gitee.io/pic/640-1601168590633.webp" alt="img"></p>
<h1 id="六、后记"><a href="#六、后记" class="headerlink" title="六、后记"></a>六、后记</h1><p>本文仅介绍的pipeline方式构建项目的过程，实际应用中还需要解决若干问题，例如：</p>
<blockquote>
<p>1、svn checkout步骤需要明文配置用户名和密码，存在安全性问题<br>2、 每次执行构建都需要全量迁出代码，效率不高<br>3、要更新的应用需要提前部署好，不能适配首次编译部署场景<br>4、对共享卷存在依赖，解耦问题需要解决</p>
</blockquote>
<h1 id="七、续"><a href="#七、续" class="headerlink" title="七、续"></a>七、续</h1><p>研究了pipeline的语法结合shell脚本，基本可以解决上述问题，以下是pipeline代码</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br></pre></td><td class="code"><pre><span class="line">podTemplate(name: &#39;jenkins-slave&#39;, cloud: &#39;kubernetes&#39;,</span><br><span class="line">  namespace: &#39;default&#39;, label: &#39;jenkins-slave&#39;) </span><br><span class="line">  &#123;</span><br><span class="line">  node(&#39;jenkins-slave&#39;) &#123;</span><br><span class="line">    stage(&#39;svn-checkout&#39;) &#123;</span><br><span class="line">      container(&#39;jnlp&#39;) &#123;</span><br><span class="line">         checkout([$class: &#39;SubversionSCM&#39;, </span><br><span class="line">         additionalCredentials: [], </span><br><span class="line">         excludedCommitMessages: &#39;&#39;, </span><br><span class="line">         excludedRegions: &#39;&#39;, </span><br><span class="line">         excludedRevprop: &#39;&#39;, </span><br><span class="line">         excludedUsers: &#39;&#39;, </span><br><span class="line">         filterChangelog: false, </span><br><span class="line">         ignoreDirPropChanges: false, </span><br><span class="line">         includedRegions: &#39;&#39;, </span><br><span class="line">         locations: [[cancelProcessOnExternalsFail: true, </span><br><span class="line">         credentialsId: &#39;ef21f2d3-cb39-468a-aa19-9175d96a365f&#39;, </span><br><span class="line">         depthOption: &#39;infinity&#39;, </span><br><span class="line">         ignoreExternalsOption: true, </span><br><span class="line">         local: &#39;.&#39;, </span><br><span class="line">         remote: &#39;http:&#x2F;&#x2F;192.168.1.81&#x2F;svn&#x2F;fjhbjsb&#x2F;k8s-pipeline-test&#39;]], </span><br><span class="line">         quietOperation: true, workspaceUpdater: [$class: &#39;UpdateUpdater&#39;]])</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    stage(&#39;mvn-package&#39;) &#123;</span><br><span class="line">      container(&#39;jnlp&#39;) &#123;</span><br><span class="line">          sh &quot;&quot;&quot;</span><br><span class="line">          mvn clean package </span><br><span class="line">          &quot;&quot;&quot;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    stage(&#39;build-docker-image&#39;) &#123;</span><br><span class="line">      container(&#39;jnlp&#39;) &#123;</span><br><span class="line">        sh &#39;&#39;&#39;</span><br><span class="line">        IMAGE_NAME&#x3D;&quot;harbor.59iedu.com&#x2F;fjhb&#x2F;jcsj:v$BUILD_NUMBER&quot;   </span><br><span class="line">        CONATINER_NAME&#x3D;&quot;jcsj-dev&quot; </span><br><span class="line">        echo &quot;192.168.1.227 harbor.59iedu.com&quot; &gt;&gt; &#x2F;etc&#x2F;hosts </span><br><span class="line">        sed -i  &quot;s&#x2F;BUILD_NUMBER&#x2F;$BUILD_NUMBER&#x2F;g&quot; tomcat.yaml</span><br><span class="line">        sed -i  &quot;s&#x2F;BUILD_NUMBER&#x3D;1&#x2F;BUILD_NUMBER&#x3D;$BUILD_NUMBER&#x2F;g&quot; rolling-update.sh </span><br><span class="line">        docker login harbor.59iedu.com -u admin -p Harbor12345 </span><br><span class="line">        docker build -t $IMAGE_NAME . </span><br><span class="line">        docker push $IMAGE_NAME</span><br><span class="line">        &#39;&#39;&#39;</span><br><span class="line"></span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    stage(&#39;apply update&#39;) &#123;</span><br><span class="line">      container(&#39;jnlp&#39;) &#123;</span><br><span class="line">          sh &#39;&#39;&#39;</span><br><span class="line">          NAMESPACE&#x3D;default</span><br><span class="line">          DEPLOYMENT&#x3D;tomcat-jcsj</span><br><span class="line">          REGISTRY&#x3D;harbor.59iedu.com&#x2F;fjhb&#x2F;jcsj:v$BUILD_NUMBER</span><br><span class="line">          FILE&#x3D;tomcat.yaml       </span><br><span class="line">          STATUS&#x3D;$(kubectl get deployment -l k8s-app&#x3D;$DEPLOYMENT -o name -n $NAMESPACE)</span><br><span class="line">          if [ ! $STATUS ];then</span><br><span class="line">          kubectl create -f $FILE</span><br><span class="line">          else</span><br><span class="line">          kubectl set image deployment&#x2F;$DEPLOYMENT $DEPLOYMENT&#x3D;$REGISTRY --namespace&#x3D;$NAMESPACE</span><br><span class="line">          fi    </span><br><span class="line">          &#39;&#39;&#39;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>其中，svn-checkout节代码通过“流水线语法”工具生成，同时需要给default sa用户相应的权限,否则无法实现apply update节配置</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"># kubectl create clusterrolebinding k8s-admin --clusterrole&#x3D;cluster-admin  --user&#x3D;system:serviceaccount:default:default</span><br></pre></td></tr></table></figure>

<p><img src="http://iubest.gitee.io/pic/640-1601168587776.webp" alt="img"><br><img src="http://iubest.gitee.io/pic/640-1601168587774.webp" alt="img"><br><img src="http://iubest.gitee.io/pic/640-1601168590779.webp" alt="img"><br><img src="http://iubest.gitee.io/pic/640-1601168587774.webp" alt="img"></p>
<p>文章转载地址如下:</p>
<ul>
<li><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">https:&#x2F;&#x2F;blog.51cto.com&#x2F;ylw6006&#x2F;2159861</span><br></pre></td></tr></table></figure></li>
</ul>
 
      <!-- reward -->
      
      <div id="reword-out">
        <div id="reward-btn">
          打赏
        </div>
      </div>
      
    </div>
    

    <!-- copyright -->
    
    <div class="declare">
      <ul class="post-copyright">
        <li>
          <i class="ri-copyright-line"></i>
          <strong>版权声明： </strong>
          
          本博客所有文章除特别声明外，著作权归作者所有。转载请注明出处！
          
        </li>
      </ul>
    </div>
    
    <footer class="article-footer">
       
<div class="share-btn">
      <span class="share-sns share-outer">
        <i class="ri-share-forward-line"></i>
        分享
      </span>
      <div class="share-wrap">
        <i class="arrow"></i>
        <div class="share-icons">
          
          <a class="weibo share-sns" href="javascript:;" data-type="weibo">
            <i class="ri-weibo-fill"></i>
          </a>
          <a class="weixin share-sns wxFab" href="javascript:;" data-type="weixin">
            <i class="ri-wechat-fill"></i>
          </a>
          <a class="qq share-sns" href="javascript:;" data-type="qq">
            <i class="ri-qq-fill"></i>
          </a>
          <a class="douban share-sns" href="javascript:;" data-type="douban">
            <i class="ri-douban-line"></i>
          </a>
          <!-- <a class="qzone share-sns" href="javascript:;" data-type="qzone">
            <i class="icon icon-qzone"></i>
          </a> -->
          
          <a class="facebook share-sns" href="javascript:;" data-type="facebook">
            <i class="ri-facebook-circle-fill"></i>
          </a>
          <a class="twitter share-sns" href="javascript:;" data-type="twitter">
            <i class="ri-twitter-fill"></i>
          </a>
          <a class="google share-sns" href="javascript:;" data-type="google">
            <i class="ri-google-fill"></i>
          </a>
        </div>
      </div>
</div>

<div class="wx-share-modal">
    <a class="modal-close" href="javascript:;"><i class="ri-close-circle-line"></i></a>
    <p>扫一扫，分享到微信</p>
    <div class="wx-qrcode">
      <img src="//api.qrserver.com/v1/create-qr-code/?size=150x150&data=http://example.com/2020/11/11/k8s/Jenkis%20pipeline%E7%BC%96%E5%86%99podTemplate%E5%AE%9E%E7%8E%B0%E5%92%8Ck8s%E5%AF%B9%E6%8E%A5/" alt="微信分享二维码">
    </div>
</div>

<div id="share-mask"></div>  
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/k8s/" rel="tag">k8s</a></li></ul>

    </footer>
  </div>

   
  <nav class="article-nav">
    
      <a href="/2020/11/11/interview/IT%E8%BF%90%E7%BB%B4%E9%9D%A2%E8%AF%95%E9%97%AE%E9%A2%98%E6%80%BB%E7%BB%93-%E5%9F%BA%E7%A1%80%E6%9C%8D%E5%8A%A1%E3%80%81%E7%A3%81%E7%9B%98%E7%AE%A1%E7%90%86%E3%80%81%E8%99%9A%E6%8B%9F%E5%B9%B3%E5%8F%B0%E5%92%8C%E7%B3%BB%E7%BB%9F%E7%AE%A1%E7%90%86/" class="article-nav-link">
        <strong class="article-nav-caption">上一篇</strong>
        <div class="article-nav-title">
          
            IT运维面试问题总结-基础服务、磁盘管理、虚拟平台和系统管理.md
          
        </div>
      </a>
    
    
      <a href="/2020/11/11/k8s/K8S%E4%BD%BF%E7%94%A8ceph-csi%E6%8C%81%E4%B9%85%E5%8C%96%E5%AD%98%E5%82%A8%E4%B9%8BCephFS/" class="article-nav-link">
        <strong class="article-nav-caption">下一篇</strong>
        <div class="article-nav-title">K8S使用ceph-csi持久化存储之CephFS.md</div>
      </a>
    
  </nav>

   
<!-- valine评论 -->
<div id="vcomments-box">
  <div id="vcomments"></div>
</div>
<script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/valine@1.4.14/dist/Valine.min.js"></script>
<script>
  new Valine({
    el: "#vcomments",
    app_id: "",
    app_key: "",
    path: window.location.pathname,
    avatar: "monsterid",
    placeholder: "给我的文章加点评论吧~",
    recordIP: true,
  });
  const infoEle = document.querySelector("#vcomments .info");
  if (infoEle && infoEle.childNodes && infoEle.childNodes.length > 0) {
    infoEle.childNodes.forEach(function (item) {
      item.parentNode.removeChild(item);
    });
  }
</script>
<style>
  #vcomments-box {
    padding: 5px 30px;
  }

  @media screen and (max-width: 800px) {
    #vcomments-box {
      padding: 5px 0px;
    }
  }

  #vcomments-box #vcomments {
    background-color: #fff;
  }

  .v .vlist .vcard .vh {
    padding-right: 20px;
  }

  .v .vlist .vcard {
    padding-left: 10px;
  }
</style>

 
     
</article>

</section>
      <footer class="footer">
  <div class="outer">
    <ul>
      <li>
        Copyrights &copy;
        2015-2020
        <i class="ri-heart-fill heart_icon"></i> TzWind
      </li>
    </ul>
    <ul>
      <li>
        
        
        
        由 <a href="https://hexo.io" target="_blank">Hexo</a> 强力驱动
        <span class="division">|</span>
        主题 - <a href="https://github.com/Shen-Yu/hexo-theme-ayer" target="_blank">Ayer</a>
        
      </li>
    </ul>
    <ul>
      <li>
        
        
        <span>
  <span><i class="ri-user-3-fill"></i>访问人数:<span id="busuanzi_value_site_uv"></span></s>
  <span class="division">|</span>
  <span><i class="ri-eye-fill"></i>浏览次数:<span id="busuanzi_value_page_pv"></span></span>
</span>
        
      </li>
    </ul>
    <ul>
      
    </ul>
    <ul>
      
    </ul>
    <ul>
      <li>
        <!-- cnzz统计 -->
        
        <script type="text/javascript" src='https://s9.cnzz.com/z_stat.php?id=1278069914&amp;web_id=1278069914'></script>
        
      </li>
    </ul>
  </div>
</footer>
      <div class="float_btns">
        <div class="totop" id="totop">
  <i class="ri-arrow-up-line"></i>
</div>

<div class="todark" id="todark">
  <i class="ri-moon-line"></i>
</div>

      </div>
    </main>
    <aside class="sidebar on">
      <button class="navbar-toggle"></button>
<nav class="navbar">
  
  <div class="logo">
    <a href="/"><img src="/images/ayer-side.svg" alt="Hexo"></a>
  </div>
  
  <ul class="nav nav-main">
    
    <li class="nav-item">
      <a class="nav-item-link" href="/">主页</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/archives">归档</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/categories">分类</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags">标签</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" target="_blank" rel="noopener" href="http://www.baidu.com">百度</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/friends">友链</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/2019/about">关于我</a>
    </li>
    
  </ul>
</nav>
<nav class="navbar navbar-bottom">
  <ul class="nav">
    <li class="nav-item">
      
      <a class="nav-item-link nav-item-search"  title="搜索">
        <i class="ri-search-line"></i>
      </a>
      
      
      <a class="nav-item-link" target="_blank" href="/atom.xml" title="RSS Feed">
        <i class="ri-rss-line"></i>
      </a>
      
    </li>
  </ul>
</nav>
<div class="search-form-wrap">
  <div class="local-search local-search-plugin">
  <input type="search" id="local-search-input" class="local-search-input" placeholder="Search...">
  <div id="local-search-result" class="local-search-result"></div>
</div>
</div>
    </aside>
    <script>
      if (window.matchMedia("(max-width: 768px)").matches) {
        document.querySelector('.content').classList.remove('on');
        document.querySelector('.sidebar').classList.remove('on');
      }
    </script>
    <div id="mask"></div>

<!-- #reward -->
<div id="reward">
  <span class="close"><i class="ri-close-line"></i></span>
  <p class="reward-p"><i class="ri-cup-line"></i>请我喝杯咖啡吧~</p>
  <div class="reward-box">
    
    
  </div>
</div>
    
<script src="/js/jquery-2.0.3.min.js"></script>


<script src="/js/lazyload.min.js"></script>

<!-- Tocbot -->


<script src="/js/tocbot.min.js"></script>

<script>
  tocbot.init({
    tocSelector: '.tocbot',
    contentSelector: '.article-entry',
    headingSelector: 'h1, h2, h3, h4, h5, h6',
    hasInnerContainers: true,
    scrollSmooth: true,
    scrollContainer: 'main',
    positionFixedSelector: '.tocbot',
    positionFixedClass: 'is-position-fixed',
    fixedSidebarOffset: 'auto'
  });
</script>

<script src="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.css">
<script src="https://cdn.jsdelivr.net/npm/justifiedGallery@3.7.0/dist/js/jquery.justifiedGallery.min.js"></script>

<script src="/dist/main.js"></script>

<!-- ImageViewer -->

<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">

    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>

    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">

        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>

        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">

            <div class="pswp__top-bar">

                <!--  Controls are self-explanatory. Order can be changed. -->

                <div class="pswp__counter"></div>

                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>

                <button class="pswp__button pswp__button--share" style="display:none" title="Share"></button>

                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>

                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>

                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                        <div class="pswp__preloader__cut">
                            <div class="pswp__preloader__donut"></div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div>
            </div>

            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>

            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>

            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>

        </div>

    </div>

</div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/default-skin/default-skin.min.css">
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe-ui-default.min.js"></script>

<script>
    function viewer_init() {
        let pswpElement = document.querySelectorAll('.pswp')[0];
        let $imgArr = document.querySelectorAll(('.article-entry img:not(.reward-img)'))

        $imgArr.forEach(($em, i) => {
            $em.onclick = () => {
                // slider展开状态
                // todo: 这样不好，后面改成状态
                if (document.querySelector('.left-col.show')) return
                let items = []
                $imgArr.forEach(($em2, i2) => {
                    let img = $em2.getAttribute('data-idx', i2)
                    let src = $em2.getAttribute('data-target') || $em2.getAttribute('src')
                    let title = $em2.getAttribute('alt')
                    // 获得原图尺寸
                    const image = new Image()
                    image.src = src
                    items.push({
                        src: src,
                        w: image.width || $em2.width,
                        h: image.height || $em2.height,
                        title: title
                    })
                })
                var gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, {
                    index: parseInt(i)
                });
                gallery.init()
            }
        })
    }
    viewer_init()
</script>

<!-- MathJax -->

<!-- Katex -->

<!-- busuanzi  -->


<script src="/js/busuanzi-2.3.pure.min.js"></script>


<!-- ClickLove -->

<!-- ClickBoom1 -->

<!-- ClickBoom2 -->

<!-- CodeCopy -->


<link rel="stylesheet" href="/css/clipboard.css">

<script src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js"></script>
<script>
  function wait(callback, seconds) {
    var timelag = null;
    timelag = window.setTimeout(callback, seconds);
  }
  !function (e, t, a) {
    var initCopyCode = function(){
      var copyHtml = '';
      copyHtml += '<button class="btn-copy" data-clipboard-snippet="">';
      copyHtml += '<i class="ri-file-copy-2-line"></i><span>COPY</span>';
      copyHtml += '</button>';
      $(".highlight .code pre").before(copyHtml);
      $(".article pre code").before(copyHtml);
      var clipboard = new ClipboardJS('.btn-copy', {
        target: function(trigger) {
          return trigger.nextElementSibling;
        }
      });
      clipboard.on('success', function(e) {
        let $btn = $(e.trigger);
        $btn.addClass('copied');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-checkbox-circle-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPIED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-checkbox-circle-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
      clipboard.on('error', function(e) {
        e.clearSelection();
        let $btn = $(e.trigger);
        $btn.addClass('copy-failed');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-time-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPY FAILED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-time-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
    }
    initCopyCode();
  }(window, document);
</script>


<!-- CanvasBackground -->


    
  </div>
</body>

</html>